// 用户 的仓库数据
import {state} from "../api/exception";
import {stringify} from "qs";
import {message} from "../utils/message";

let api = require("@/api").default;


export default {
    namespaced: true,
    state: {
        isLoading: false,
        tags:{},
    },
    getters:{

    },
    mutations:{
        /**
         * 设置加载状态
         */
        setIsLoading(state, payload){
            state.isLoading = payload;
        },
        /**
         * 设置标签
         */
        setTags(state, payload){
            state.tags[payload.uid] = payload.tags;
        },
    },
    actions:{
        /**
         * 添加标签
         */
        async addTags(context, payload){
            context.commit("setIsLoading", true);

            let resp = await api.tag.addTag(payload);
            if(resp.code === state.Success){
                await message.success("添加成功！")
                resp.data == [] ? message.warning("标签：[" + resp.data + "]已存在！") : null;
            }else{
                message.warning(resp.message);
            }

            context.commit("setIsLoading", false);
        },
        /**
         * 删除标签
         */
        async delTag(context, payload){
            context.commit("setIsLoading", true);

            let resp = await api.tag.delTag(payload);
            if(resp.code === state.Success){
                if (resp.data) {
                    await message.success("删除成功！")
                }
            }else{
                message.warning(resp.message);
            }

            context.commit("setIsLoading", false);
        },

        /**
         * 获取标签
         */
        async listTags(context, payload){
            context.commit("setIsLoading", true);
            let resp = await api.tag.listTags({uid: payload});
            if(resp.code === state.Success){
                context.commit("setTags", {uid: payload, tags: resp.data});
            }else{
                message.warning(resp.message);
            }

            context.commit("setIsLoading", false);
        },
    },
};